########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
import os
from glob import glob
import logging

import conf



def name(fullname):
    """Get the file name without directory and extension"""
    return os.path.splitext(os.path.split(fullname)[1])[0]

def build_path(sub,subdirs=[],bdir=None):
    return conf.build_path(sub,subdirs,bdir)

def root(f=[]):
    return build_path(None,f)

def flt(f=[]):
    return build_path('flt',f)

def script(f=[]):
    return build_path('script',f)

def imports(f=[]):
    return build_path('imports',f)

def outputs(f=[]):
    return build_path('outputs',f)

def templates(f=[]):
    return build_path('templates',f)

def temp(f=[]):
    return build_path('temp',f)

def globLine(line,def_file=[]):
    if isinstance(def_file,str): def_file=[def_file]
    globs = line.pop('FILE',def_file)
    filenames = []
    for g in globs:
        filenames.extend(glob(g))
    if len(filenames) == 0:
        logging.warning("No matching file found matching %s",globs)
    return filenames

def home(f=[], create=False):
    """Return the home directory for pyfm.

    The PYFM_HOME environment variable if it exists.
    Else, try ~/.pyfm, if it does not expand use the current directory.

    f - additional path components
    create - whether to create the resulting path if it does not exist
    """
    return conf.home(f,create)

if __name__=='__main__': print home()
